package com.nero.service.impl;

import com.nero.bean.PO.TbContent;
import com.nero.bean.PO.TbContentCategory;
import com.nero.bean.PO.TbContentCategoryExample;
import com.nero.bean.PO.TbContentExample;
import com.nero.bean.VO.Slide;
import com.nero.common.pojo.EUTreeNode;
import com.nero.dao.TbContentCategoryMapper;
import com.nero.dao.TbContentMapper;
import com.nero.service.IContentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Nero on 18/8/7.
 */
@Service
public class ContentServiceImpl implements IContentService{
    @Resource
    TbContentCategoryMapper tbContentCategoryMapper;
    @Resource
    TbContentMapper tbContentMapper;

    @Override
    public List<EUTreeNode> findTreeNodes(long parent_id) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria c = example.createCriteria();
        c.andParentIdEqualTo(parent_id);
        List<TbContentCategory> tbList = tbContentCategoryMapper.selectByExample(example);
        List<EUTreeNode> nodeList = new ArrayList<>();
        for (TbContentCategory tbContentCategory : tbList) {
            EUTreeNode node = new EUTreeNode();
            node.setId(tbContentCategory.getId());
            node.setText(tbContentCategory.getName());
            node.setState(tbContentCategory.getIsParent()?"closed":"open");
            nodeList.add(node);
        }
        return nodeList;
    }

    @Override
    public void createCategory(String name, long parentId) {

    }

    @Override
    public List<TbContent> getContentListByCid(long cid) {
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(cid);
        List<TbContent> list = tbContentMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<Slide> getSlides() {
        List<TbContent> contents = tbContentMapper.selectByExample(new TbContentExample());
        List<Slide> slides = new ArrayList<>();
        for (TbContent content : contents) {
            Slide slide = new Slide();
            slide.setSrc(content.getPic());
            slide.setSrcB(content.getPic2());
            slide.setAlt(content.getTitle());
            slide.setHref(content.getUrl());
            slide.setHeight(240);
            slide.setWidth(670);
            slide.setHeightB(240);
            slide.setWidthB(550);
            slides.add(slide);
        }
        return slides;
    }
}
